from urllib.parse import urlencode

from dadashop import settings


class OauthWeiboApi:
    def __init__(self, app_key, app_secret, redirect_uri):
        self.app_key = app_key
        self.app_secret = app_secret
        self.redirect_uri = redirect_uri

    def get_auth_url(self):
        """
        获取授权地址
        """
        url = "https://api.weibo.com/oauth2/authorize"
        params = {
            'client_id': self.app_key,
            'redirect_uri': self.redirect_uri,
            'response_type': 'code',

        }
        return url + '?' + urlencode(params)+"&forcelogin=true"
    def get_access_token_url(self,code):
        """
        获取令牌地址
        """

        data = {
            'client_id': self.app_key,
            'client_secret': self.app_secret,
            'grant_type': 'authorization_code',
            'redirect_uri': self.redirect_uri,
            'code': code,
        }
        return data

if __name__ == '__main__':
    api = OauthWeiboApi(app_key='842124378',
                        app_secret='1fb034aceec39ec8fa79b4c2930d3e7d',
                        redirect_uri='http://localhost:7000/dadashop/templates/callback.html')
    print(api.get_auth_url())